package com.sky.controller.admin;

import com.sky.constant.CacheKeyRuleConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController("adminDishController")
@Slf4j
@RequestMapping("/admin/dish")
@Api(tags = "菜品管理接口")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 新增菜品
     *
     * @param dishDTO
     * @return
     */
    @ApiOperation("新增菜品")
    @PostMapping
    public Result save(@RequestBody DishDTO dishDTO) {
        log.info("新增菜品 : {}", dishDTO);
        dishService.save(dishDTO);

        //清理缓存数据
        String key = CacheKeyRuleConstant.DISH_KEY + dishDTO.getCategoryId();
        cleanCache(key);

        return Result.success();
    }

    /**
     * 条件分页查询
     *
     * @param pageQueryDTO
     * @return
     */
    @ApiOperation("条件分页查询")
    @GetMapping("/page")
    public Result<PageResult> page(DishPageQueryDTO pageQueryDTO) {
        log.info("条件分页查询 : {}", pageQueryDTO);
        PageResult pageResult = dishService.pageQuery(pageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 批量删除菜品
     *
     * @param ids
     * @return
     */
    @ApiOperation("批量删除菜品")
    @DeleteMapping
    public Result deleteByIds(@RequestParam List<Long> ids) {
        log.info("批量删除菜品，参数:{}", ids);
        dishService.deleteByIds(ids);

        // 清空所有的菜品缓存数据
        cleanCache(CacheKeyRuleConstant.CLEAN_DISH_KEY);

        return Result.success();
    }

    /**
     * 根据 id 查询信息 - 修改页面回显
     *
     * @param id
     * @return
     */
    @ApiOperation("根据 id 查询信息")
    @GetMapping("/{id}")
    public Result<DishVO> getInfoById(@PathVariable Long id) {
        log.info("根据 id 查询菜品信息");
        DishVO dishVO = dishService.getInfoById(id);
        return Result.success(dishVO);
    }

    /**
     * 菜品信息编辑
     *
     * @param dishDTO
     * @return
     */
    @ApiOperation("菜品信息编辑")
    @PutMapping
    public Result editInfo(@RequestBody DishDTO dishDTO) {
        log.info("菜品信息编辑 : {}", dishDTO);
        dishService.editInfo(dishDTO);

        // 清空菜品缓存数据
        cleanCache(CacheKeyRuleConstant.CLEAN_DISH_KEY);

        return Result.success();
    }

    /**
     * 获取该分类的菜品列表 - 用于套餐管理的增加套餐中选择菜品时显示
     *
     * @param categoryId
     * @return
     */
    @ApiOperation("获取该分类的菜品列表")
    @GetMapping("/list")
    public Result<List<Dish>> listByCategoryId(Long categoryId) {
        log.info("获取该分类的菜品列表 : {}", categoryId);
        List<Dish> dishList = dishService.listByCategoryId(categoryId);
        return Result.success(dishList);
    }

    /**
     * 菜品启售|停售
     *
     * @param status
     * @param id
     * @return
     */
    @ApiOperation("菜品启售|停售")
    @PostMapping("/status/{status}")
    public Result enableOrDisable(@PathVariable Integer status, Long id) {
        log.info("菜品启售|停售,设置的状态为: {}，id:{}", status == 1 ? "启售" : "停售", id);
        dishService.enableOrDisable(status, id);

        // 清空菜品缓存数据
        cleanCache(CacheKeyRuleConstant.CLEAN_DISH_KEY);

        return Result.success();
    }

    /**
     * 清理缓存数据
     * @param pattern
     */
    private void cleanCache(String pattern) {
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }
}
